package io.gitee.zhangbinhub.acp.cloud.oauth.controller

import cn.dev33.satoken.annotation.SaCheckPermission
import cn.dev33.satoken.annotation.SaCheckRole
import cn.dev33.satoken.context.SaHolder
import cn.dev33.satoken.oauth2.annotation.SaCheckAccessToken
import cn.dev33.satoken.oauth2.annotation.SaCheckClientToken
import cn.dev33.satoken.oauth2.data.model.AccessTokenModel
import cn.dev33.satoken.oauth2.data.model.ClientTokenModel
import cn.dev33.satoken.oauth2.processor.SaOAuth2ServerProcessor
import cn.dev33.satoken.stp.StpUtil
import io.gitee.zhangbinhub.acp.cloud.resource.server.tools.TokenTools
import io.swagger.annotations.Api
import org.noear.solon.annotation.Controller
import org.noear.solon.annotation.Mapping
import org.noear.solon.validation.annotation.Valid

@Api("控制器")
@Valid
@Controller
open class OauthController {
    // OAuth2-Server 端：处理所有 OAuth2 相关请求
    @Mapping("/oauth/*")
    fun request(): Any {
        println("------- 进入请求: " + SaHolder.getRequest().url)
        return SaOAuth2ServerProcessor.instance.dister()
    }

    @SaCheckRole("ADMIN")
    @SaCheckPermission("system")
    @SaCheckAccessToken(scope = ["ALL"])
    @Mapping("/token-info")
    fun tokenInfo(): AccessTokenModel? {
        val permissionList = StpUtil.getPermissionList()
        val roleList = StpUtil.getRoleList()
        println(StpUtil.getTokenInfo().toString())
        return TokenTools.getAccessTokenModel()
    }

    @SaCheckClientToken
    @Mapping("/client/token-info")
    fun clientTokenInfo(): ClientTokenModel? {
        return TokenTools.getClientTokenModel()
    }

    @Mapping("/open/token-info")
    fun openTokenInfo(): String {
        return "ok"
    }
}